#!/usr/bin/python 
#Author:eager.yu
#Email: sanguren@qq.com

from Cloudstack_option import Cloudstack_option
import sys,re
import getopt


def usage():
    print "help: id is instance id,the create processing will stopped instance and reboot it when complate"
    print "python createTemplate.py -i 367a50b8-ae03-48c6-8f44-f2e4d7be7dfd"
    print ""

def main():
    id=""
    try:
        opts, args = getopt.getopt(sys.argv[1:], "hi:",["help", "id="])
    except getopt.GetoptError as err:
        print str(err) 
        usage()
        sys.exit(2)
    for o,a in opts:
        if o in ("-h", "--help"):
            usage()
            sys.exit(0)
        elif o in ("-i", "--id"):
            id=a
        else:
            assert False, "unhandled option"
    if id == "" :
        print "id can't be null"
        sys.exit(0)
    args={"id":id}
    cs=Cloudstack_option()
    instances=cs.get_all_instance("")
    instances_id=[]
    for i in range(len(instances)):
        instances_id.append(instances[i]["id"])
    if not id in instances_id:
        print "the instance is not  in cloudstack"
        sys.exit()
    instance_info=cs.get_instance(args)
    jobs=cs.create_template(instance_info)
    print jobs
    sys.exit(0)


if __name__ == "__main__":
    main()